// pages/balanceDetail/balanceDetail.js
var api = require('../../utils/api.js');
var app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    page_star:0,
    balenceList:{},
    btn_txt:'点击获取更多'
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    

    try {
      var balenceList = wx.getStorageSync('balenceList')
      if (balenceList) {
        var page_star = wx.getStorageSync('balance_page_star')
        var btn_txt = wx.getStorageSync('balance_btn_txt')        
        
        that.setData({
          balenceList: balenceList,
          page_star: page_star,
          btn_txt: btn_txt
        });
      } else {
        this.getbanaList();
      }
    } catch (e) {
      this.getbanaList();
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.getbanaList();
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  /**
   * 获取异动列表
   */
  getbanaList: function (refresh = 1){
    var that = this;
    //起始数
    var page_star = that.data.page_star;
    if (refresh == 1){
      page_star = 0
    }

    api.post({
      url:'user/balance/getBaList',
      data: { page_star: page_star},
      success:function(res){
        console.log(res);
        if(res.code){
          var data = res.data;

          //更新起始值
          if (refresh){
            page_star = data.length;
          }else{
            page_star += data.length;
          }
          //检查是否是下拉刷新数据
          var newList = (refresh == 1)?data:that.data.balenceList.concat(data);

          //底部按钮文字
          if (data.length < 10){ //没有更多了
            var btn_txt = '没有更多了';
          }else{
            var btn_txt = '点击获取更多';
          }

          that.setData({
            balenceList: newList,
            page_star: page_star,
            btn_txt: btn_txt
          })



          //加入缓存
          wx.setStorage({
            key: "balenceList",
            data: newList
          })
          wx.setStorage({
            key: "balance_page_star",
            data: page_star
          })
          wx.setStorage({
            key: "balance_btn_txt",
            data: btn_txt
          })

          wx.showToast({
            title: res.msg,
            icon: 'success',
            duration: 1000
          });
        }else{
          wx.showToast({
            title: res.msg,
            icon: 'error',
            duration: 1000
          });
        }
      }
    })
  },
  /**
   * 点击获取更多
   */
  getMore:function(){
    this.getbanaList(0);
  }
})